﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SZSLSystem.Model;
using SZSLSystem.DBUtility;

namespace SZSLSystem.DAL
{
    /// <summary>
    /// 数据访问层：权限
    /// </summary>
    public partial class Authority
    {
        public Authority()
        {

        }

        #region  Method

        /// <summary>
        /// 判断城市编号是否存在
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public bool Exists(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Authority");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        ///  返回数据总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H ");
            strSql.Append(" from Authority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }

        /// <summary>
        /// 增加城市
        /// </summary>
        /// <param name="model">城市实体</param>
        /// <returns></returns>
        public int Add(SZSLSystem.Model.Authority model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Authority(");
            strSql.Append("ID,AuthorityName,Abbreviation,ParentID,ClassLayer,Label,Description,Status,Sort)");
            strSql.Append(" values (");
            strSql.Append("@ID,@AuthorityName,@Abbreviation,@ParentID,@ClassLayer,@Label,@Description,@Status,@Sort)");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.UniqueIdentifier),
					new SqlParameter("@AuthorityName", SqlDbType.VarChar,50),
                    new SqlParameter("@Abbreviation", SqlDbType.VarChar,50),
                    new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier),
                    new SqlParameter("@ClassLayer", SqlDbType.Int,4),
                    new SqlParameter("@Label", SqlDbType.Int,4),
                    new SqlParameter("@Description", SqlDbType.VarChar,500),
					new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.AuthorityName;
            parameters[2].Value = model.Abbreviation;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.ClassLayer;
            parameters[5].Value = model.Label;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Sort;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新城市
        /// </summary>
        /// <param name="model">城市实体</param>
        /// <returns></returns>
        public bool Update(SZSLSystem.Model.Authority model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Authority set ");
            strSql.Append("AuthorityName=@AuthorityName,");
            strSql.Append("Abbreviation=@Abbreviation,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("ClassLayer=@ClassLayer,");
            strSql.Append("Label=@Label,");
            strSql.Append("Description=@Description,");
            strSql.Append("Status=@Status,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.UniqueIdentifier),
					new SqlParameter("@AuthorityName", SqlDbType.VarChar,50),
                    new SqlParameter("@Abbreviation", SqlDbType.VarChar,50),
                    new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier),
                    new SqlParameter("@ClassLayer", SqlDbType.Int,4),
                    new SqlParameter("@Label", SqlDbType.Int,4),
                    new SqlParameter("@Description", SqlDbType.VarChar,500),
					new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.AuthorityName;
            parameters[2].Value = model.Abbreviation;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.ClassLayer;
            parameters[5].Value = model.Label;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Sort;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除城市
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public bool Delete(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete Authority ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据城市编号获取数据
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public SZSLSystem.Model.Authority GetModel(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Authority ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            SZSLSystem.Model.Authority model = new SZSLSystem.Model.Authority();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ID = Guid.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                model.AuthorityName = ds.Tables[0].Rows[0]["AuthorityName"].ToString();
                model.Abbreviation = ds.Tables[0].Rows[0]["Abbreviation"].ToString();
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = Guid.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                model.ClassLayer = int.Parse(ds.Tables[0].Rows[0]["ClassLayer"].ToString());
                model.Label = int.Parse(ds.Tables[0].Rows[0]["Label"].ToString());
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得城市数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from Authority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得城市前几行数据
        /// </summary>
        /// <param name="Top">行数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" from Authority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得城市查询分页数据
        /// </summary>
        /// <param name="pageSize">每页数目</param>
        /// <param name="currentPage">当前页数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder)
        {
            int topSize = pageSize * currentPage;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " * from Authority");
            strSql.Append(" where Id not in(select top " + topSize + " Id from Authority");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + ")");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到所有商品类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetTreeList(Guid id)
        {
            DataTable data = new DataTable();
            //创建列
            DataColumn ID = new DataColumn("ID", typeof(Guid));
            DataColumn AuthorityName = new DataColumn("AuthorityName", typeof(string));
            DataColumn Abbreviation = new DataColumn("Abbreviation", typeof(string));
            DataColumn ParentID = new DataColumn("ParentID", typeof(Guid));
            DataColumn ClassLayer = new DataColumn("ClassLayer", typeof(int));
            DataColumn Status = new DataColumn("Status", typeof(int));
            DataColumn Sort = new DataColumn("Sort", typeof(int));
            //添加列
            data.Columns.Add(ID);
            data.Columns.Add(AuthorityName);
            data.Columns.Add(Abbreviation);
            data.Columns.Add(ParentID);
            data.Columns.Add(ClassLayer);
            data.Columns.Add(Status);
            data.Columns.Add(Sort);
            GetChild(data, id);
            return data;
        }

        /// <summary>
        /// 取得该类别下所有类别
        /// </summary>
        /// <param name="data">DATATABLE名</param>
        /// <param name="ParID">父ID</param>
        private void GetChild(DataTable data, Guid ParID)
        {
            StringBuilder str = new StringBuilder();
            str.Append("select * from Authority");
            str.Append(" where ParentID=@ParentID ");
            str.Append(" order by Sort asc");
            SqlParameter[] parameters = {
					new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = ParID;
            DataSet ds = DbHelperSQL.Query(str.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    //添加一行数据
                    DataRow row = data.NewRow();
                    row[0] = Guid.Parse(dr["ID"].ToString());
                    row[1] = dr["AuthorityName"].ToString();
                    row[2] = dr["Abbreviation"].ToString();
                    row[3] = Guid.Parse(dr["ParentID"].ToString());
                    row[4] = int.Parse(dr["ClassLayer"].ToString());
                    row[5] = int.Parse(dr["Status"].ToString());
                    row[6] = int.Parse(dr["Sort"].ToString());
                    data.Rows.Add(row);
                    //调用自身迭代
                    this.GetChild(data, Guid.Parse(dr["ID"].ToString()));
                }
            }
        }

        public DataTable GetGetTree(Guid id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = id;
            return DbHelperSQL.RunProcedure("AuthorityTree", parameters, "AuthorityTree").Tables[0];
        }

        #endregion  Method
    }
}
